<template>
  <div id="app">
    <div v-if="!network">
      <h3>我没网了</h3>
      <div @click="onRefresh">刷新</div>
    </div>
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"/>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'App',
  watch:{
    '$route':function(to,from){
      let token = localStorage.getItem('token');
      if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
          this.$router.replace('/login')
      }
    }
  },
  computed: {
    ...mapState(['network'])
  },
  methods: {
    onRefresh () {
      this.$router.replace('/refresh')
    }
  }
}
</script>

<style>

</style>
